package com.guigu.qingcheng.controller;

import com.alibaba.fastjson.JSON;
import com.guigu.qingcheng.pojo.TbOrder;
import com.guigu.qingcheng.pojo.TbSku;
import com.guigu.qingcheng.pojo.TbUser;
import com.guigu.qingcheng.pojo.dto.OrderItemSku;
import com.guigu.qingcheng.service.GoodsServiceFeign;
import com.guigu.qingcheng.service.OrderServiceFeign;
import com.guigu.qingcheng.service.UserServiceFeign;
import com.guigu.qingcheng.utils.JwtUtils;
import com.guigu.qingcheng.utils.R;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
public class OrderController {
    @Autowired
    private OrderServiceFeign orderServiceFeign;
    @Autowired
    private GoodsServiceFeign goodsServiceFeign;
    @Autowired
    private UserServiceFeign userServiceFeign;

    @GetMapping("/queryOrderItemSkuByUsername/{pageNum}/{pageSize}/{orderStatus}")
    @ResponseBody
    public R queryOrderItemSkuByUsername(HttpServletRequest request, @PathVariable("pageNum") Integer pageNum
            , @PathVariable("pageSize") Integer pageSize,@PathVariable("orderStatus") Integer orderStatus) {
        String username = JwtUtils.getMemberIdByJwtToken(request);
        return orderServiceFeign.queryOrderItemSkuByUsername(username, pageNum, pageSize,orderStatus);
    }

    //跳转我的订单页面
    @GetMapping("/myOrder")
    public String openMyOrder(Model model) {
        // 查询出最高销量的四件商品
        R r = goodsServiceFeign.findSkuBySaleNum();
        // 通过json将数据转换成 List<TbSku> 对象
        List<TbSku> skuBySaleNum = JSON.parseArray(JSON.toJSONString(r.getData().get("skuBySaleNum")), TbSku.class);
        model.addAttribute("skuBySaleNum", skuBySaleNum);
        return "center-index";
    }



    @GetMapping("/orderDetail/{id}")
    public String orderDetail(@PathVariable String id, Model model){
        // 通过订单Id 获取到订单数据
        R r = orderServiceFeign.detail(id);
        // 通过订单Id 获取到三表查询数据
        R r1 = orderServiceFeign.queryOrderItemSkuByOrderId(id);
        // 查询出最高销量的四件商品
        R r2 = goodsServiceFeign.findSkuBySaleNum();
        // 通过json将数据转换成 OrderItemSku 对象
        OrderItemSku orderItemSku = JSON.parseObject(JSON.toJSONString(r1.getData().get("orderItemSku")), OrderItemSku.class);
        // 通过json将数据转换成 TbOrder 对象
        TbOrder tbOrder = JSON.parseObject(JSON.toJSONString(r.getData().get("tbOrder")), TbOrder.class);
        // 通过json将数据转换成 List<TbSku> 对象
        List<TbSku> skuBySaleNum = JSON.parseArray(JSON.toJSONString(r2.getData().get("skuBySaleNum")), TbSku.class);
        // 把对象封装进model中
        model.addAttribute("order", tbOrder);
        model.addAttribute("orderItemSku", orderItemSku);
        model.addAttribute("skuBySaleNum", skuBySaleNum);
        // 返回跳转到页面
        return "center-order-detail";
    }

    @GetMapping("/findSkuBySaleNum")
    @ResponseBody
    public R findSkuBySaleNum() {
        return goodsServiceFeign.findSkuBySaleNum();
    }

    @GetMapping("/centerSettingSafe")
    public String centerSettingSafe() {
        return "center-setting-safe";
    }

    @PostMapping("/updatePasswordByUsername")
    @ResponseBody
    public R updatePasswordByUsername(@RequestBody TbUser user) {
        return userServiceFeign.updatePasswordByUsername(user);
    }

    @ApiOperation(value = "订单确认收货 已完成订单")
    @GetMapping("/okOrder/{id}")
    public R okOrder(@PathVariable String id) {
        return orderServiceFeign.okOrder(id);
    }

}
